package com.soulkun.smarthome.ui.main.personal.adapter

import android.content.Context
import android.graphics.Typeface
import android.graphics.drawable.ColorDrawable
import android.text.Spannable
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.style.AbsoluteSizeSpan
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.view.View
import androidx.core.content.res.ResourcesCompat
import androidx.databinding.ObservableArrayList
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.LinearLayoutManager
import com.soulkun.smarthome.R
import com.soulkun.smarthome.databinding.AppRecycleItemMainPersonalMessageAlertContainerListBinding
import com.soulkun.smarthome.databinding.AppRecycleItemMainPersonalMessageFamilyContainerListBinding
import com.soulkun.smarthome.logic.model.FamilyModel
import com.soulkun.smarthome.logic.vo.AlertMessageHistoryVO
import com.soulkun.smarthome.logic.vo.FamilyMessageHistoryVO
import soulkun.library.helper.decoration.LinearSpaceItemDecorationHelper
import soulkun.library.mvvm.adapter.BaseDataBindingRecyclerViewAdapter
import soulkun.library.utils.getAppResources
import soulkun.library.utils.time.DateUtils
import soulkun.library.utils.toPx
import java.sql.Date

class AppMainPersonalMessageFamilyContainerListAdapter(
    context: Context,
    dataList: ObservableArrayList<FamilyMessageHistoryVO>
) :
    BaseDataBindingRecyclerViewAdapter<FamilyMessageHistoryVO>(context, dataList) {

    override fun getContentLayoutResId(): Int {
        return R.layout.app_recycle_item_main_personal_message_family_container_list
    }

    override fun setContentViewHolderCreateListener(binding: ViewDataBinding) {
    }

    override fun setContentViewHolderBindListener(binding: ViewDataBinding, dataPosition: Int) {
        val data = dataList[dataPosition]
        val dateString = DateUtils.dateToStringWithFormat(Date(data.dateTime), "dd日 MM月yyyy年")
        val spannableString = SpannableString(dateString).apply {
            val boldSpan = StyleSpan(Typeface.BOLD)
            val blackSpan = ForegroundColorSpan(ResourcesCompat.getColor(getAppResources(), R.color.app_black_19, null))
            val graySpan = ForegroundColorSpan(ResourcesCompat.getColor(getAppResources(), R.color.app_gray_80, null))
            val dp18Span = AbsoluteSizeSpan(context.resources.getDimensionPixelSize(R.dimen.sw_18dp))
            val dp12Span = AbsoluteSizeSpan(context.resources.getDimensionPixelSize(R.dimen.sw_12dp))
            setSpan(blackSpan, 0, 3, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
            setSpan(dp18Span, 0, 3, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
            setSpan(boldSpan, 0, 3, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
            setSpan(graySpan, 4, 12, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
            setSpan(dp12Span, 4, 12, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
        }
        (binding as AppRecycleItemMainPersonalMessageFamilyContainerListBinding).tvMainPersonalMessageFamilyContainerListTextDate.text = spannableString
        binding.rvMainPersonalMessageFamilyContainerListListMessage.run {
            layoutManager = LinearLayoutManager(context)
            adapter = AppMainPersonalMessageFamilyContainerMessageListAdapter(context, data.messageList)
            if (itemDecorationCount == 0) {
                addItemDecoration(LinearSpaceItemDecorationHelper(10, 10, 0, 0, 10))
            }
        }
    }

}